﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace ScreenConditioner
{
    /// <summary>
    /// Interaction logic for ConditionWindow.xaml
    /// Cycle through color red, green, blue, black, white
    /// </summary>
    public partial class ConditionWindow : Window
    {
        DispatcherTimer _timer;
        SolidColorBrush[] _brushes = new SolidColorBrush[] 
            { 
                new SolidColorBrush(new Color { A=255, R=255, G=0, B=0 }),
                new SolidColorBrush(new Color { A=255, R=0, G=255, B=0 }),
                new SolidColorBrush(new Color { A=255, R=0, G=0, B=255 }),
                new SolidColorBrush(new Color { A=255, R=0, G=0, B=0 }),
                new SolidColorBrush(new Color { A=255, R=255, G=255, B=255}),
            };
        int _iCurrentColorIndex = 0;

        public ConditionWindow()
        {
            InitializeComponent();

            Properties.Settings.Default.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Default_PropertyChanged);

            _timer = new DispatcherTimer();
            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Interval = TimeSpan.FromMilliseconds(Properties.Settings.Default.CycleTime);
            this.Background = _brushes[_iCurrentColorIndex];
        }

        void Default_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "CycleTime")
            {
                if (Properties.Settings.Default.CycleTime < 100)
                    Properties.Settings.Default.CycleTime = 100;

                _timer.Interval = TimeSpan.FromMilliseconds(Properties.Settings.Default.CycleTime);
            }
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            _iCurrentColorIndex++;
            if (_iCurrentColorIndex >= _brushes.Length)
                _iCurrentColorIndex = 0;
            this.Background = _brushes[_iCurrentColorIndex];
        }

        public void StartTimer()
        {
            _timer.Start();
        }

        public void StopTimer()
        {
            _timer.Stop();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                // Toggle the maximize state
                this.WindowState = (this.WindowState == WindowState.Maximized) ? WindowState.Normal : WindowState.Maximized;
            }

            if ((e.ButtonState == System.Windows.Input.MouseButtonState.Pressed) && (e.ChangedButton == System.Windows.Input.MouseButton.Left) && (e.ClickCount == 1))
            {
                this.DragMove();
            }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.EnableWindow = false;
        }


    }
}
